package com.gugee.ins.common.dal.service.product.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.product.InsProductExtMapper;
import com.gugee.ins.common.dal.service.product.InsProductDao;
import com.gugee.ins.common.model.mysql.product.InsProduct;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.gugee.ins.data.common.msg.InsProductMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * insBlogger dao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsProductDaoImpl extends ServiceImpl<InsProductExtMapper, InsProduct> implements InsProductDao {

    /**
     * 查找是否存在，存在则返回
     * @param productIds
     * @return
     */
    @Override
    public List<Long> findExistUid(List<Long> productIds) {
        return baseMapper.findExistUid(productIds);
    }

    @Override
    public List<InsProduct> findByProductIds(List<Long> productIds){
        return baseMapper.findByProductIds(productIds);
    }

    /**
     * 批量插入
     * @param products
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<InsProduct> products) {
        try{
            this.saveBatch(products,products.size() + 1);
        }catch (Exception e){
            log.debug("batch insert ins product failed ,try insert one by one, msg : {} ",e.getMessage());
            products.forEach(product -> {
                try{
                    baseMapper.insert(product);
                }catch (Exception ex){
                    log.info("insert ins product failed,productId:{}, msg : {} "
                            ,product.getProductId(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 批量更新，暂时只更新最近发文时间字段
     * @param products
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatch(List<InsProduct> products) {
        long now = System.currentTimeMillis() / 1000;
        try{
            baseMapper.batchUpdateMediaTagTime(products,now);
        }catch (Exception e){
            log.debug("batch update ins product failed ,try update one by one, msg : {} ",e.getMessage());
            products.forEach(product -> {
                try{
                    baseMapper.updateMediaTagTime(product,now,false);
                }catch (Exception ex){
                    log.info("update ins product failed,productId:{}, msg : {} "
                            ,product.getProductId(),ex.getMessage());
                }
            });
        }
    }

    /**
     * 更新
     * @param product
     */
    @Override
    public void updateMediaTagTime(InsProduct product,boolean withClTime) {
        long now = System.currentTimeMillis() / 1000;
        baseMapper.updateMediaTagTime(product,now,withClTime);
    }

    /**
     * 获取最大id
     * @return
     */
    @Override
    public long getMaxId() {
        return baseMapper.getMaxId();
    }

    /**
     * 获取任务列表
     * @param startId
     * @param endId
     * @param limitTime
     * @return
     */
    @Override
    public List<InsBloggerMessage> findInsProductMessageByPages(long startId, long endId, long limitTime) {
        return baseMapper.findInsProductMessageByPages(startId,endId,limitTime);
    }

    /**
     * 更新商品分类
     * @param pid
     * @param cate
     */
    @Override
    public void updateCate(long pid, String cate) {
        baseMapper.updateCate(pid,cate,System.currentTimeMillis() / 1000);
    }

    /**
     * 获取分析商品分类任务列表
     * @param startId
     * @param endId
     * @param limitTime
     * @return
     */
    @Override
    public List<InsProductMessage> findInsAnalysisProductCategoryMsgByPages(long startId, long endId, long limitTime) {
        return baseMapper.findInsAnalysisProductCategoryMsgByPages(startId,endId,limitTime);
    }

    /**
     * 根据productId 查找
     * @param productId
     * @return
     */
    @Override
    public InsProduct findByProductId(long productId){
        return baseMapper.findByProductId(productId);
    }
}
